package com.adonis.ejoy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.adonis.ejoy.Item.MyPic;
import com.adonis.ejoy.Item.PopPic;
import com.adonis.ejoy.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class MyPicAdapter extends RecyclerView.Adapter<MyPicAdapter.ViewHolder> {
    private List<MyPic> mMyPicList;
//    private OnPopPicTouchListener mOnPopPicTouchListener;

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView MyPicImage;
        View MyPicView;

        public ViewHolder(View view){
            super(view);
            MyPicView=view;
            MyPicImage=view.findViewById(R.id.poppic_image);
        }
    }

    public MyPicAdapter(List<MyPic> popPicList){
        mMyPicList = popPicList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.poppic_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.MyPicView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position=holder.getAdapterPosition();
                //variable "holder" is accessed from within inner class,needs to be declared to be final
                //java中规定，内部类只能访问外部类中的成员变量，不能访问方法中定义的变量
                MyPic myPic=mMyPicList.get(position);
                //这里设置相应的跳转
                Toast.makeText(v.getContext(),"you clicked view",Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MyPic myPic = mMyPicList.get(position);
        WindowManager wm=(WindowManager) holder.itemView.getContext().getSystemService(Context.WINDOW_SERVICE);
        int width=wm.getDefaultDisplay().getWidth()/3;    //得到屏幕宽度的1/3
        int avatarSize = width;
        Picasso.with(holder.itemView.getContext())                     //指定图片的父布局
                .load(myPic.getImageId())
                .placeholder(R.drawable.img_circle_placeholder)      //在网络请求还没有完成时显示的图片，它必须是本地图片
                .resize(avatarSize, avatarSize)                      //设置图片大小
                .centerCrop()                                           //居中裁剪
                .into(holder.MyPicImage);
        //holder.PopPicImage.setImageResource(popPic.getImageId());
    }

    @Override
    public int getItemCount() {
        return mMyPicList.size();
    }

}
